//
//  YQUserAccount.m
//  WeiboOC
//
//  Created by yang on 15/11/15.
//  Copyright © 2015年 yang. All rights reserved.
//

#import "YQUserAccount.h"

@implementation YQUserAccount
+ (instancetype)userAccountWithDict:(NSDictionary*)dict
{
    YQUserAccount* user = [[self alloc] init];
    user.access_token = dict[@"access_token"];
    user.expires_in = dict[@"expires_in"];
    user.uid = dict[@"uid"];
    user.avatar_large = dict[@"avatar_large"];
    user.name = dict[@"name"];
    return user;
}
// 归档
- (void)saveAccount{
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"account.plist"];
    [NSKeyedArchiver archiveRootObject:self toFile:path];

}
// 解档
- (YQUserAccount *)loadAccount{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"account.plist"];
   YQUserAccount *accout = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    return accout;
}
// 归档协议
- (void)encodeWithCoder:(NSCoder*)aCoder
{
    [aCoder encodeObject:_access_token forKey:@"access_token"];
    [aCoder encodeObject:_expires_in forKey:@"expires_in"];
    [aCoder encodeObject:_uid forKey:@"uid"];
    [aCoder encodeObject:_avatar_large forKey:@"avatar_large"];
    [aCoder encodeObject:_name forKey:@"name"];
}
// 解档协议
- (instancetype)initWithCoder:(NSCoder*)aDecoder
{
    self = [super init];
    if (self) {
        _access_token = [aDecoder decodeObjectForKey:@"access_token"];
        _expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
        _uid = [aDecoder decodeObjectForKey:@"uid"];
        _avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
        _name = [aDecoder decodeObjectForKey:@"name"];
    }
    return self;
}
@end
